#include <bits/stdc++.h>

using namespace std;
typedef long long LL;
LL str2ll(char str[],LL len,LL mp[],LL r)
{
    LL ans=0;
    for (LL i=0;i<len ;++i )
    {
        ans=ans*r+mp[(int)str[i]];
    }
    return ans;
}
LL binarySearch(char str[],LL mp[],LL left_r,LL right_r,LL x)
{
    LL mid,temp;
    while (left_r<right_r)
    {
        mid=left_r+(right_r-left_r)/2;
        temp=str2ll(str,strlen(str),mp,mid);
        if (temp>x)
        {
            right_r=mid-1;
        }
        else
        {
            left_r=mid+1;
        }
    }
    temp=str2ll(str,strlen(str),mp,left_r);
    if (temp>x)
    {
        return -1;
    }
    else
    {
        return left_r;
    }
}
int main_1010_1()
{
    char N1[11]={0},N2[11]={0};
    LL tag,radix,base=2;
    LL hash_table[123]={0};
    for (LL i=0; i<10; ++i)
    {
        hash_table[i+'0']=i;
    }
    for (LL i=10;i<36 ;++i )
    {
        hash_table[i+'W']=i;
    }
    scanf("%s%s%lld%lld",N1,N2,&tag,&radix);
    if (tag==2)
    {
        swap(N1,N2);
    }
    //N1 is the tag
    int N1Num=str2ll(N1,strlen(N1),hash_table,radix);
    for (LL i=0;i<strlen(N2) ;++i )
    {
        if ((hash_table[(int)N2[i]]+1)>base)
        {
            base=hash_table[(int)N2[i]]+1;
        }
    }
    LL ans=binarySearch(N2,hash_table,base,36,N1Num);
    if (ans!=-1)
    {
        printf("%lld",ans);
    }
    else
    {
        printf("Impossible");
    }
    return 0;
}
